package com.hkts.naturalgas.utils;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @ClassNameBezierPoint
 * @Description
 * @Author yck
 * @Date2021-06-08 11:21
 * @Version V1.0
 **/
public class BezierPoint {
    public static double variance(double[] x) {
        int m = x.length;
        double sum = 0;
        //求和
        for (int i = 0; i < m; i++) {
            sum += x[i];
        }

        //求平均值
        double dAve = sum / m;
        // System.out.println(String.format("平均值= %s", dAve));
        double dVar = 0;
        //求方差
        for (int i = 0; i < m; i++) {
            dVar += (x[i] - dAve) * (x[i] - dAve);
        }
        //System.out.println(String.format("方差= %s", dVar / m));
        return dVar / m;
    }

    //标准差σ=sqrt(s^2)
    public static double standardDiviation(double[] x) {
        int m = x.length;
        double sum = 0;
        //求和
        for (int i = 0; i < m; i++) {
            sum += x[i];
        }

        //求平均值
        double dAve = sum / m;
        double dVar = 0;

        //求方差
        for (int i = 0; i < m; i++) {
            dVar += (x[i] - dAve) * (x[i] - dAve);
        }
        //System.out.println(String.format("平均值= %s", dAve));
        // System.out.println(String.format("方差= %s", dVar / m));
        //System.out.println(String.format("标准差= %s", Math.sqrt(dVar / m)));
        return Math.sqrt(dVar / m);
    }


    public static void getBezierPoints(int num, double point1, double point2) {
        System.out.println(String.format("------------------------"));
        double[] pointList = {};
        double xs = point1;
        double xe = point2;
//        float xc = point3;

        int t = 0;

        double xd = xe - xs;
        //System.out.println(String.format("差值 = %s", xd));

        double xa1 = (xs + xe) / 2;
        //System.out.println(String.format("平均数1 = %s", xa1));

        double xa2 = (xs + xa1) / 2;
       // System.out.println(String.format("平均数2= %s", xa2));

        double xa3 = (xe + xa1) / 2;
        //System.out.println(String.format("平均数3= %s", xa3));

        double xc = xa3;

        for (int i = 0; i < num; i++) {
            t = i / num;
            double x = (double) (Math.pow(1 - t, 2) * xs + 2 * t * (1 - t) * xc + Math.pow(t, 2) * xe);
           // System.out.println(String.format("%s", x ));

        }
    }

    public static List<Float> generateGaussine(int num, double variance, double average) throws ParseException {
        DecimalFormat df = new DecimalFormat("#.00");
        List<Float> list=new ArrayList<>();
        for (int i = 0; i < num; i++) {
            Random randomno = new Random();
            double val = randomno.nextGaussian() * Math.sqrt(variance)+average;
             System.out.println(val);
                String format = df.format((float)val);
                list.add(Float.parseFloat(format) );
        }
         return list;
    }
    public static boolean getRundoum(){
        Random rm = new Random();
       int value= rm.nextInt(100)+1;
       if(value%2==0){
           return false;
       }
        return true;
    }
   /** 功能描述:
   *获取是否为无小数点
   * @return: void
   * @Author: 杨春科
   * @Date: 2021-06-08 17:05
   * @Description //TODO
   */
    public static void main(String[] args) throws ParseException {
        double[] inputs = {110, 0d};
        double total = 0;
        for (int i = 0; i < inputs.length; i++) {
            total += inputs[i];
        }
        double average = total / inputs.length;
        // System.out.println(average);
//        standardDiviation(inputs);
        generateGaussine(60, variance(inputs), average);
//        System.out.println(variance(inputs));
//        System.out.println(standardDiviation(inputs));
    }

    public static List<Float> getData(float var1,float var2) throws ParseException {
        double[] inputs = {var1, var2};
        double total = 0;
        for (int i = 0; i < inputs.length; i++) {
            total += inputs[i];
        }
        double average = total / inputs.length;
        // System.out.println(average);
        //standardDiviation(inputs);
        List<Float> list= generateGaussine(60, variance(inputs), average);
        return list;
    }
}
